﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using System.Device.Location;

namespace CommunityMegaphone
{
    public partial class MainView : PhoneApplicationPage
    {
        public MainView()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            var searchResults = App.SearchResultsViewModel;
            if (searchResults == null)
                return;

            if ((object)DataContext == null)
                DataContext = searchResults;

            switch (searchResults.SearchType)
            {
                default:
                    tbSubTitle.Text = "events";
                    break;
                case SearchType.WellKnownCity:
                    tbSubTitle.Text = String.Format("events near {0}", 
                        searchResults.SearchCityName);
                    break;
                case SearchType.CurrentLocation:
                    tbSubTitle.Text = "events near you";
                    break;
                case SearchType.ZipCode:
                    tbSubTitle.Text = String.Format("events near ZIP {0}", 
                        searchResults.PostalCode);
                    break;
            }
        }

        private void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            // If selected index is -1 (no selection) do nothing
            if (MainListBox.SelectedIndex == -1)
                return;

            // Navigate to the new page
            NavigationService.Navigate(new Uri("/Views/DetailsView.xaml?selectedItem="
                + MainListBox.SelectedIndex, UriKind.Relative));

            // Reset selected index to -1 (no selection)
            MainListBox.SelectedIndex = -1;
        }

        private void OnLoadNearbyEvents(object sender, EventArgs e)
        {
            GeoCoordinate center = App.CurrentGeoLocation;

            if (center == null || 
                App.CurrentGeoStatus != GeoPositionStatus.Ready || 
                App.CurrentGeoLocation.IsUnknown)
            {
                if (MessageBox.Show("Select a well-known city instead?", 
                    "Location Unknown", MessageBoxButton.OKCancel) != MessageBoxResult.OK)
                    return;
                OnSearchByCity(this, null);
                return;
            }

            App.SearchResultsViewModel.SearchType = SearchType.CurrentLocation;
            App.SearchResultsViewModel.ReloadEvents(center);
        }

        private void OnSearchByCity(object sender, EventArgs e)
        {
            App.CitiesViewModel.Filter = String.Empty;
            NavigationService.Navigate(new Uri("/Views/CitiesView.xaml", UriKind.Relative));
        }

        private void OnReloadAll(object sender, EventArgs e)
        {
            App.SearchResultsViewModel.SearchType = SearchType.All;
            tbSubTitle.Text = "all events";
            App.SearchResultsViewModel.ReloadEvents(true);
        }
    }
}
